package org.asiainfo.file.config;

import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.asiainfo.common.core.exception.ServiceException;
import org.asiainfo.file.config.properties.MinioProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * minio配置
 *
 * @author dotor-ww
 */
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
@Slf4j
public class MinioConfig {

    /**
     * 创建 minio 客户端
     *
     * @param minioProperties minio配置
     * @return 客户端
     */
    @Bean
    public MinioClient minioClient(MinioProperties minioProperties) {
        try {
            return MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        } catch (Exception e) {
            throw new ServiceException("minio客户端创建失败 ==> " + e.getMessage());
        }
    }
}
